package fy.hr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.json.JSONObject;

import fy.config.FyConfig;
import fy.util.DBUtil;
import fy.util.MyUtil;


public class LeaveInstead_FindUserInfoDao {

	private String insteadCode;
	private String leaveMemberCode;

	public void setInsteadCode(String insteadCode) {
		this.insteadCode = insteadCode;
	}

	public void setLeaveMemberCode(String leaveMemberCode) {
		this.leaveMemberCode = leaveMemberCode;
	}

	public String query() {
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		String result = null;
		try {
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());

			st = conn
					.prepareStatement("select tab1.psncode,tab1.psnname,tab2.deptname,tab3.jobname,tab4.basgroupdef4 from bd_psndoc tab1 join bd_deptdoc tab2 on tab1.pk_deptdoc=tab2.pk_deptdoc join om_job tab3 on tab1.pk_om_job=tab3.pk_om_job join bd_psnbasdoc tab4 on tab1.pk_psnbasdoc=tab4.pk_psnbasdoc where tab1.psnclscope in ('0','5') and tab1.psncode=?");

			st.setString(1, leaveMemberCode);

			rs = st.executeQuery();
			if (rs.next()) {
				JSONObject jo = new JSONObject();
				jo.put("姓名1", rs.getString(2));
				jo.put("所在部门1", rs.getString(3));
				jo.put("岗位1", rs.getString(4));
				jo.put("进厂时间年月", rs.getString(5));
				jo.put("工号1", rs.getString(1));

				DBUtil.close(rs, st);

				st = conn
						.prepareStatement("select t1.restdayorhour from TBM_LMHOLIDAY t1, bd_psndoc t2,tbm_timeitem t3 where t2.psnclscope in ('0', '5') and t2.psncode=? and t1.pk_corp=t2.pk_corp and t1.pk_psndoc = t2.pk_psndoc and t3.timeitemname='年假' and t3.pk_timeitem=t1.pk_timeitem and  t1.curyear=?");
				st.setString(1, leaveMemberCode);
				st.setString(2, MyUtil.getYear());

				rs = st.executeQuery();
				if (rs.next()) {
					jo.put("年休节余天数1", rs.getString(1));
					jo.put("截止时间1", MyUtil.getNowStr());
				}
				DBUtil.close(rs, st);

				st = conn
						.prepareStatement("select tab1.psncode,tab1.psnname,tab3.jobname from bd_psndoc tab1 join om_job tab3 on tab1.pk_om_job=tab3.pk_om_job where tab1.psnclscope in ('0','5') and tab1.psncode=?");
				st.setString(1, insteadCode);

				rs = st.executeQuery();
				if (rs.next()) {
					jo.put("代填人姓名1", rs.getString(1));
					jo.put("代填人工号1", rs.getString(2));
					jo.put("代填人岗位1", rs.getString(3));
				}
				DBUtil.close(rs, st);

				jo.put("success", true);
				result = jo.toString();
			} else {
				result = "{success:false,msg:\"未查询到请假人信息，原因：\n1、工号输入错误；\2、EHR系统中未给请假人排班。\"}";
			}

		} catch (Exception e) {
			e.printStackTrace();
			result = "{success:false,msg:\"查询请假人员信息发生异常，请重试，或者联系OA系统管理员。\"}";
		} finally {
			DBUtil.close(rs, st, conn);
		}

		return result;
	}
}
